package com.ln.property_control.controller;

import com.ln.property_control.pojo.*;
import com.ln.property_control.service.CheckPlanService;
import com.ln.property_control.utils.GetId;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/*创建处理盘点计划控制器*/
@Controller
@RequestMapping("CheckPlan")
public class CheckPlanController {

    @Autowired
    private CheckPlanService checkPlanService;

    /*展示所有盘点计划并完成分页事务*/
    @ResponseBody
    @RequestMapping("/selecyAllPlan")
    public Map<String, Object> selecyAllPlan(Integer page, Integer limit) {
        Map<String, Object> map = checkPlanService.selecyAllPlan(page, limit);
        return map;
    }

    /*展示指定盘点计划详情的方法*/
    /*展示所有盘点计划并完成分页事务*/
    @RequestMapping("/selectByCheckID")
    public String selectByCheckID(String checkplanID, Model model, HttpSession session) {
        CheckPlanVo checkPlanVo = checkPlanService.selectByCheckID(checkplanID);
        List<PropertyVo> propertyVoList = checkPlanService.selectPropertyByID(checkplanID);
        List<Department> departmentList = checkPlanService.selectDeByID();
        /*获取到指定计划下的盈亏备注*/
        List<Result_remark> remarkList = checkPlanService.findCheckID(checkplanID);
        User userInfo = (User) session.getAttribute("userInfo");
        System.out.println(userInfo);
        model.addAttribute("userInfo", userInfo);
        model.addAttribute("checkPlanVo", checkPlanVo);
        model.addAttribute("departmentList", departmentList);
        model.addAttribute("propertyVoList", propertyVoList);
        model.addAttribute("remarkList", remarkList);
        return "asset/check/checkparticulars";
    }

    /*提报新盘点计划*/
    @RequestMapping("/goaddCheckPlan")
    public String goaddCheckPlan(Model model) {
        List<Department> departmentList = checkPlanService.selectDeByID();
        List<PropertyVo> propertyVoList = checkPlanService.selectProTypeByID();
        List<CheckPlanType> checkPlanTypeList = checkPlanService.selectCheckTypeByID();
        model.addAttribute("departmentList", departmentList);
        model.addAttribute("propertyVoList", propertyVoList);
        model.addAttribute("checkPlanTypeList", checkPlanTypeList);
        return "asset/check/addCheckPlan";
    }

    /*获取指定部门指定类型的资产*/
    @ResponseBody
    @RequestMapping("selectByDepIDorTypeID")
    public List<PropertyVo> selectByDepIDorTypeID(Integer property_dep_id, Integer property_tid) {
        List<PropertyVo> propertyVoList = checkPlanService.selectByDepIDorTypeID(property_dep_id, property_tid);
        return propertyVoList;
    }

    /*新增盘点计划*/
    @ResponseBody
    @RequestMapping("addCheckPlan")
    public String addCheckPlan(CheckPlan checkPlan) {
        String checkplan_id = "PD_" + GetId.getTime().replaceAll("[[\\s-:punct:]]", "") + GetId.getRandomNum();
        checkPlan.setCheckplan_id(checkplan_id);
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(date);
        checkPlan.setPlanstart_time(format);
        checkPlanService.addCheckPlan(checkPlan);


        History_plan history_plan = new History_plan();
        history_plan.setHistory_plan_id(checkplan_id);
        history_plan.setHistory_plan_name(checkPlan.getCheckplan_name());
        history_plan.setPlan_person_name(checkPlan.getParson_man());
        history_plan.setFinal_result_id(1);
        String hisDate = simpleDateFormat.format(new Date());
        history_plan.setApprover_time(hisDate);
        history_plan.setFinal_approver_name(" ");
        String history_id = "RZ_" + GetId.getTime().replaceAll("[[\\s-:punct:]]", "") + GetId.getRandomNum();
        history_plan.setHistory_id(history_id);
        checkPlanService.insertAccPlan(history_plan);
        return "ok";
    }

    /*更改计划的通过状态*/
    @ResponseBody
    @RequestMapping("updatePassCheck")
    public String updatePassCheck(CheckPlan checkPlan,History_plan history_plan, Integer rankNum) {
        /*判断当前计划审核时候已经完毕*/
        if (checkPlan.getCheck_plan_statu() == 3 || checkPlan.getCheck_plan_statu() == 4) {
            return "审批已结束！";
        } else {
            /*首先判断当前审批人员的级别与审批流程是否不符*/
            if (checkPlan.getCheck_plan_statu() == 1) {//当前应当一级审核员审核
                if (rankNum == 1) {//当前审核员为一级人员
                    checkPlan.setCheck_plan_statu(2);/*修改状态为等待二级审核*/
                    checkPlanService.updateCheck(checkPlan);


                    /*将操作添加到日志中*/
                    history_plan.setHistory_plan_id(checkPlan.getCheckplan_id());
                    String history_id = "RZ_" + GetId.getTime().replaceAll("[[\\s-:punct:]]", "") + GetId.getRandomNum();
                    history_plan.setFinal_result_id(2);
                    Date date = new Date();
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    String format = simpleDateFormat.format(date);
                    history_plan.setApprover_time(format);
                    history_plan.setHistory_id(history_id);
                    checkPlanService.insertAccPlan(history_plan);


                    return "审批成功！谢谢你的决策！";
                } else if (rankNum == 2) {
                    checkPlan.setCheck_plan_statu(4);/*修改状态为已通过*/
                    checkPlanService.updateCheck(checkPlan);


                    /*将已完成的计划加入历史表并从计划表中清楚，并将操作添加到日志中*/
                    String history_id = "RZ_" + GetId.getTime().replaceAll("[[\\s-:punct:]]", "") + GetId.getRandomNum();
                    history_plan.setFinal_result_id(4);
                    Date date = new Date();
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    String format = simpleDateFormat.format(date);
                    history_plan.setApprover_time(format);
                    history_plan.setHistory_id(history_id);
                    checkPlanService.exmaCheckPlan(checkPlan, history_plan);


                    return "审批成功！谢谢你的决策！";
                } else {
                    return "您无权操作，谢谢！";
                }
            } else if (checkPlan.getCheck_plan_statu() == 2) {
                if (rankNum == 1) {//当前审核员为一级人员
                    return "您已完成审批，请勿重复审批！";
                } else if (rankNum == 2) {//当前审核员为二级人员
                    checkPlan.setCheck_plan_statu(4);/*修改状态为已通过*/
                    checkPlanService.updateCheck(checkPlan);


                    /*将已完成的计划加入历史表并从计划表中清楚，并将操作添加到日志中*/
                    String history_id = "RZ_" + GetId.getTime().replaceAll("[[\\s-:punct:]]", "") + GetId.getRandomNum();
                    history_plan.setFinal_result_id(4);
                    Date date = new Date();
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    String format = simpleDateFormat.format(date);
                    history_plan.setApprover_time(format);
                    history_plan.setHistory_id(history_id);
                    checkPlanService.exmaCheckPlan(checkPlan, history_plan);

                    return "审批成功！谢谢你的决策！";
                } else {
                    return "您无权操作，谢谢！";
                }
            }
        }
        return "";
    }
    /*更改计划的通过状态*/
    @ResponseBody
    @RequestMapping("updateForbidCheck")
    public String updateForbidCheck(CheckPlan checkPlan, History_plan history_plan, Integer rankNum) {
        if(rankNum==1 || rankNum ==2){/*判断当前审批人的等级身份*/
            /*判断当前如果是以及审批人并且已经完成了审批*/
            checkPlan.setCheck_plan_statu(3);
            checkPlanService.updateCheck(checkPlan);
            /*将已完成的计划加入历史表并从计划表中清楚，并将操作添加到日志中*/
            String history_id = "RZ_" + GetId.getTime().replaceAll("[[\\s-:punct:]]", "") + GetId.getRandomNum();
            history_plan.setFinal_result_id(3);
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String format = simpleDateFormat.format(date);
            history_plan.setApprover_time(format);
            history_plan.setHistory_id(history_id);
            System.out.println(history_plan);
            checkPlanService.exmaCheckPlan(checkPlan, history_plan);


            return "审批成功！谢谢你的决策！";
        }else{
            return "您无权操作谢谢";
        }
    }

    /*获取到所有日志*/
    @ResponseBody
    @RequestMapping("initHistroy")
    public Map<String, Object> selectHistory(Integer page , Integer  limit,
                                             @RequestParam(required = false ,defaultValue = "") String approver_time,
                                             @RequestParam(required = false ,defaultValue = "") String history_plan_name){
        return checkPlanService.selectHistory(page, limit,approver_time,history_plan_name);
    }

    /*公司总资产展示*/
    @ResponseBody
    @RequestMapping("selectAllProperty")
    public Map<String, Object> selectAllProperty(Integer page , Integer  limit,
                                                 @RequestParam(required = false ,defaultValue = "") Integer present_dep_id,
                                                 @RequestParam(required = false ,defaultValue = "") String property_name){
        return checkPlanService.selectAllProperty(page, limit,present_dep_id,property_name);
    }

    /*创建查询等待一级审批的计划*/
    @ResponseBody
    @RequestMapping("selectByDep")
    public Map<String, Object> selectByDep(HttpSession session,Integer page , Integer  limit,@RequestParam(required = false,defaultValue = "") Integer did){
        User userInfo = (User) session.getAttribute("userInfo");
        Integer rankNum = 0;
        if(userInfo.getRankNum()==1||userInfo.getRankNum()==2){
            rankNum=userInfo.getRankNum();
        }else{

        }
        return checkPlanService.selectByDep( page, limit, did,rankNum);
    }

    /*添加新备注表*/
    @ResponseBody
    @RequestMapping("addResMar")
    public String addResMar(Result_remark result_remark){
        checkPlanService.addResMar(result_remark);
        return "ok";
    }

}

